
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  
<!-- Mirrored from werkzeug.palletsprojects.com/en/1.0.x/quickstart/ by HTTrack Website Copier/3.x [XR&CO'2014], Tue, 15 Sep 2020 06:37:08 GMT -->
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Quickstart &#8212; Werkzeug Documentation (1.0.x)</title>
    <link rel="stylesheet" href="../_static/werkzeug.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" type="text/css" href="../../../../assets.readthedocs.org/static/css/badge_only.css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script async="async" type="text/javascript" src="../../../../assets.readthedocs.org/static/javascript/readthedocs-doc-embed.js"></script>
    <link rel="shortcut icon" href="../_static/favicon.ico"/>
    <link rel="index" title="Index" href="../genindex/index.html" />
    <link rel="search" title="Search" href="../search/index.html" />
    <link rel="next" title="Serving WSGI Applications" href="../serving/index.html" />
    <link rel="prev" title="API Levels" href="../levels/index.html" />
    <link rel="canonical" href="index.html">
  <script>DOCUMENTATION_OPTIONS.URL_ROOT = '../index.html';</script>
   
  
<!-- RTD Extra Head -->

<!-- 
Always link to the latest version, as canonical.
http://docs.readthedocs.org/en/latest/canonical.html
-->
<link rel="canonical" href="index.html" />

<link rel="stylesheet" href="../../../../assets.readthedocs.org/static/css/readthedocs-doc-embed.css" type="text/css" />

<script type="text/javascript" src="../_static/readthedocs-data.js"></script>

<!-- Add page-specific data, which must exist in the page js, not global -->
<script type="text/javascript">
READTHEDOCS_DATA['page'] = "quickstart"
READTHEDOCS_DATA['source_suffix'] = ".rst"
</script>

<script type="text/javascript" src="../../../../assets.readthedocs.org/static/javascript/readthedocs-analytics.js" async="async"></script>

<!-- end RTD <extrahead> -->
</head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex/index.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex/index.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../serving/index.html" title="Serving WSGI Applications"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="../levels/index.html" title="API Levels"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">Werkzeug Documentation (1.0.x)</a> &#187;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="quickstart">
<h1>Quickstart<a class="headerlink" href="#quickstart" title="Permalink to this headline">¶</a></h1>
<p>This part of the documentation shows how to use the most important parts of
Werkzeug.  It’s intended as a starting point for developers with basic
understanding of <span class="target" id="index-0"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0333"><strong>PEP 333</strong></a> (WSGI) and <span class="target" id="index-1"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2616.html"><strong>RFC 2616</strong></a> (HTTP).</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p>Make sure to import all objects from the places the documentation
suggests.  It is theoretically possible in some situations to import
objects from different locations but this is not supported.</p>
<p class="last">For example <code class="xref py py-class docutils literal notranslate"><span class="pre">MultiDict</span></code> is a member of the <cite>werkzeug</cite> module
but internally implemented in a different one.</p>
</div>
<div class="section" id="wsgi-environment">
<h2>WSGI Environment<a class="headerlink" href="#wsgi-environment" title="Permalink to this headline">¶</a></h2>
<p>The WSGI environment contains all the information the user request transmits
to the application.  It is passed to the WSGI application but you can also
create a WSGI environ dict using the <code class="xref py py-func docutils literal notranslate"><span class="pre">create_environ()</span></code> helper:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">werkzeug.test</span> <span class="k">import</span> <span class="n">create_environ</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">environ</span> <span class="o">=</span> <span class="n">create_environ</span><span class="p">(</span><span class="s1">&#39;/foo&#39;</span><span class="p">,</span> <span class="s1">&#39;http://localhost:8080/&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Now we have an environment to play around:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;PATH_INFO&#39;</span><span class="p">]</span>
<span class="go">&#39;/foo&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;SCRIPT_NAME&#39;</span><span class="p">]</span>
<span class="go">&#39;&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;SERVER_NAME&#39;</span><span class="p">]</span>
<span class="go">&#39;localhost&#39;</span>
</pre></div>
</div>
<p>Usually nobody wants to work with the environ directly because it is limited
to bytestrings and does not provide any way to access the form data besides
parsing that data by hand.</p>
</div>
<div class="section" id="enter-request">
<h2>Enter Request<a class="headerlink" href="#enter-request" title="Permalink to this headline">¶</a></h2>
<p>For access to the request data the <code class="xref py py-class docutils literal notranslate"><span class="pre">Request</span></code> object is much more fun.
It wraps the <cite>environ</cite> and provides a read-only access to the data from
there:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">werkzeug.wrappers</span> <span class="k">import</span> <span class="n">Request</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">request</span> <span class="o">=</span> <span class="n">Request</span><span class="p">(</span><span class="n">environ</span><span class="p">)</span>
</pre></div>
</div>
<p>Now you can access the important variables and Werkzeug will parse them
for you and decode them where it makes sense.  The default charset for
requests is set to <cite>utf-8</cite> but you can change that by subclassing
<code class="xref py py-class docutils literal notranslate"><span class="pre">Request</span></code>.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">request</span><span class="o">.</span><span class="n">path</span>
<span class="go">u&#39;/foo&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">request</span><span class="o">.</span><span class="n">script_root</span>
<span class="go">u&#39;&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">request</span><span class="o">.</span><span class="n">host</span>
<span class="go">&#39;localhost:8080&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">request</span><span class="o">.</span><span class="n">url</span>
<span class="go">&#39;http://localhost:8080/foo&#39;</span>
</pre></div>
</div>
<p>We can also find out which HTTP method was used for the request:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">request</span><span class="o">.</span><span class="n">method</span>
<span class="go">&#39;GET&#39;</span>
</pre></div>
</div>
<p>This way we can also access URL arguments (the query string) and data that
was transmitted in a POST/PUT request.</p>
<p>For testing purposes we can create a request object from supplied data
using the <code class="xref py py-meth docutils literal notranslate"><span class="pre">from_values()</span></code> method:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">cStringIO</span> <span class="k">import</span> <span class="n">StringIO</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="s2">&quot;name=this+is+encoded+form+data&amp;another_key=another+one&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">request</span> <span class="o">=</span> <span class="n">Request</span><span class="o">.</span><span class="n">from_values</span><span class="p">(</span><span class="n">query_string</span><span class="o">=</span><span class="s1">&#39;foo=bar&amp;blah=blafasel&#39;</span><span class="p">,</span>
<span class="gp">... </span>   <span class="n">content_length</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">input_stream</span><span class="o">=</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="p">),</span>
<span class="gp">... </span>   <span class="n">content_type</span><span class="o">=</span><span class="s1">&#39;application/x-www-form-urlencoded&#39;</span><span class="p">,</span>
<span class="gp">... </span>   <span class="n">method</span><span class="o">=</span><span class="s1">&#39;POST&#39;</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">request</span><span class="o">.</span><span class="n">method</span>
<span class="go">&#39;POST&#39;</span>
</pre></div>
</div>
<p>Now we can access the URL parameters easily:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">request</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
<span class="go">[&#39;blah&#39;, &#39;foo&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">request</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="s1">&#39;blah&#39;</span><span class="p">]</span>
<span class="go">u&#39;blafasel&#39;</span>
</pre></div>
</div>
<p>Same for the supplied form data:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">request</span><span class="o">.</span><span class="n">form</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">]</span>
<span class="go">u&#39;this is encoded form data&#39;</span>
</pre></div>
</div>
<p>Handling for uploaded files is not much harder as you can see from this
example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">store_file</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="n">file</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">files</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;my_file&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">file</span><span class="p">:</span>
        <span class="n">file</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="s1">&#39;/where/to/store/the/file.txt&#39;</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">handle_the_error</span><span class="p">()</span>
</pre></div>
</div>
<p>The files are represented as <code class="xref py py-class docutils literal notranslate"><span class="pre">FileStorage</span></code> objects which provide
some common operations to work with them.</p>
<p>Request headers can be accessed by using the <code class="xref py py-class docutils literal notranslate"><span class="pre">headers</span></code>
attribute:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">request</span><span class="o">.</span><span class="n">headers</span><span class="p">[</span><span class="s1">&#39;Content-Length&#39;</span><span class="p">]</span>
<span class="go">&#39;54&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">request</span><span class="o">.</span><span class="n">headers</span><span class="p">[</span><span class="s1">&#39;Content-Type&#39;</span><span class="p">]</span>
<span class="go">&#39;application/x-www-form-urlencoded&#39;</span>
</pre></div>
</div>
<p>The keys for the headers are of course case insensitive.</p>
</div>
<div class="section" id="header-parsing">
<h2>Header Parsing<a class="headerlink" href="#header-parsing" title="Permalink to this headline">¶</a></h2>
<p>There is more.  Werkzeug provides convenient access to often used HTTP headers
and other request data.</p>
<p>Let’s create a request object with all the data a typical web browser transmits
so that we can play with it:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">environ</span> <span class="o">=</span> <span class="n">create_environ</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">environ</span><span class="o">.</span><span class="n">update</span><span class="p">(</span>
<span class="gp">... </span>    <span class="n">HTTP_USER_AGENT</span><span class="o">=</span><span class="s1">&#39;Mozilla/5.0 (Macintosh; U; Mac OS X 10.5; en-US; ) Firefox/3.1&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="n">HTTP_ACCEPT</span><span class="o">=</span><span class="s1">&#39;text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="n">HTTP_ACCEPT_LANGUAGE</span><span class="o">=</span><span class="s1">&#39;de-at,en-us;q=0.8,en;q=0.5&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="n">HTTP_ACCEPT_ENCODING</span><span class="o">=</span><span class="s1">&#39;gzip,deflate&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="n">HTTP_ACCEPT_CHARSET</span><span class="o">=</span><span class="s1">&#39;ISO-8859-1,utf-8;q=0.7,*;q=0.7&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="n">HTTP_IF_MODIFIED_SINCE</span><span class="o">=</span><span class="s1">&#39;Fri, 20 Feb 2009 10:10:25 GMT&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="n">HTTP_IF_NONE_MATCH</span><span class="o">=</span><span class="s1">&#39;&quot;e51c9-1e5d-46356dc86c640&quot;&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="n">HTTP_CACHE_CONTROL</span><span class="o">=</span><span class="s1">&#39;max-age=0&#39;</span>
<span class="gp">... </span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">request</span> <span class="o">=</span> <span class="n">Request</span><span class="p">(</span><span class="n">environ</span><span class="p">)</span>
</pre></div>
</div>
<p>Let’s start with the most useless header: the user agent:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">request</span><span class="o">.</span><span class="n">user_agent</span><span class="o">.</span><span class="n">browser</span>
<span class="go">&#39;firefox&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">request</span><span class="o">.</span><span class="n">user_agent</span><span class="o">.</span><span class="n">platform</span>
<span class="go">&#39;macos&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">request</span><span class="o">.</span><span class="n">user_agent</span><span class="o">.</span><span class="n">version</span>
<span class="go">&#39;3.1&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">request</span><span class="o">.</span><span class="n">user_agent</span><span class="o">.</span><span class="n">language</span>
<span class="go">&#39;en-US&#39;</span>
</pre></div>
</div>
<p>A more useful header is the accept header.  With this header the browser
informs the web application what mimetypes it can handle and how well.  All
accept headers are sorted by the quality, the best item being the first:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">request</span><span class="o">.</span><span class="n">accept_mimetypes</span><span class="o">.</span><span class="n">best</span>
<span class="go">&#39;text/html&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;application/xhtml+xml&#39;</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">accept_mimetypes</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span> <span class="n">request</span><span class="o">.</span><span class="n">accept_mimetypes</span><span class="p">[</span><span class="s2">&quot;application/json&quot;</span><span class="p">]</span>
<span class="go">0.8</span>
</pre></div>
</div>
<p>The same works for languages:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">request</span><span class="o">.</span><span class="n">accept_languages</span><span class="o">.</span><span class="n">best</span>
<span class="go">&#39;de-at&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">request</span><span class="o">.</span><span class="n">accept_languages</span><span class="o">.</span><span class="n">values</span><span class="p">()</span>
<span class="go">[&#39;de-at&#39;, &#39;en-us&#39;, &#39;en&#39;]</span>
</pre></div>
</div>
<p>And of course encodings and charsets:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;gzip&#39;</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">accept_encodings</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">request</span><span class="o">.</span><span class="n">accept_charsets</span><span class="o">.</span><span class="n">best</span>
<span class="go">&#39;ISO-8859-1&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;utf-8&#39;</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">accept_charsets</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Normalization is available, so you can safely use alternative forms to perform
containment checking:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;UTF8&#39;</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">accept_charsets</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;de_AT&#39;</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">accept_languages</span>
<span class="go">True</span>
</pre></div>
</div>
<p>E-tags and other conditional headers are available in parsed form as well:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">request</span><span class="o">.</span><span class="n">if_modified_since</span>
<span class="go">datetime.datetime(2009, 2, 20, 10, 10, 25)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">request</span><span class="o">.</span><span class="n">if_none_match</span>
<span class="go">&lt;ETags &#39;&quot;e51c9-1e5d-46356dc86c640&quot;&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">request</span><span class="o">.</span><span class="n">cache_control</span>
<span class="go">&lt;RequestCacheControl &#39;max-age=0&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">request</span><span class="o">.</span><span class="n">cache_control</span><span class="o">.</span><span class="n">max_age</span>
<span class="go">0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;e51c9-1e5d-46356dc86c640&#39;</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">if_none_match</span>
<span class="go">True</span>
</pre></div>
</div>
</div>
<div class="section" id="responses">
<h2>Responses<a class="headerlink" href="#responses" title="Permalink to this headline">¶</a></h2>
<p>Response objects are the opposite of request objects.  They are used to send
data back to the client.  In reality, response objects are nothing more than
glorified WSGI applications.</p>
<p>So what you are doing is not <em>returning</em> the response objects from your WSGI
application but <em>calling</em> it as WSGI application inside your WSGI application
and returning the return value of that call.</p>
<p>So imagine your standard WSGI “Hello World” application:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">application</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">):</span>
    <span class="n">start_response</span><span class="p">(</span><span class="s1">&#39;200 OK&#39;</span><span class="p">,</span> <span class="p">[(</span><span class="s1">&#39;Content-Type&#39;</span><span class="p">,</span> <span class="s1">&#39;text/plain&#39;</span><span class="p">)])</span>
    <span class="k">return</span> <span class="p">[</span><span class="s1">&#39;Hello World!&#39;</span><span class="p">]</span>
</pre></div>
</div>
<p>With response objects it would look like this:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">werkzeug.wrappers</span> <span class="k">import</span> <span class="n">Response</span>

<span class="k">def</span> <span class="nf">application</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">):</span>
    <span class="n">response</span> <span class="o">=</span> <span class="n">Response</span><span class="p">(</span><span class="s1">&#39;Hello World!&#39;</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">response</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">)</span>
</pre></div>
</div>
<p>Also, unlike request objects, response objects are designed to be modified.
So here is what you can do with them:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">werkzeug.wrappers</span> <span class="k">import</span> <span class="n">Response</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span> <span class="o">=</span> <span class="n">Response</span><span class="p">(</span><span class="s2">&quot;Hello World!&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">headers</span><span class="p">[</span><span class="s1">&#39;content-type&#39;</span><span class="p">]</span>
<span class="go">&#39;text/plain; charset=utf-8&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">data</span>
<span class="go">&#39;Hello World!&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">headers</span><span class="p">[</span><span class="s1">&#39;content-length&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">data</span><span class="p">)</span>
</pre></div>
</div>
<p>You can modify the status of the response in the same way.  Either just the
code or provide a message as well:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">status</span>
<span class="go">&#39;200 OK&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">status</span> <span class="o">=</span> <span class="s1">&#39;404 Not Found&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">status_code</span>
<span class="go">404</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">status_code</span> <span class="o">=</span> <span class="mi">400</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">status</span>
<span class="go">&#39;400 BAD REQUEST&#39;</span>
</pre></div>
</div>
<p>As you can see attributes work in both directions.  So you can set both
<code class="xref py py-attr docutils literal notranslate"><span class="pre">status</span></code> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">status_code</span></code> and the
change will be reflected to the other.</p>
<p>Also common headers are exposed as attributes or with methods to set /
retrieve them:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">content_length</span>
<span class="go">12</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">datetime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">date</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2009</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">17</span><span class="p">,</span> <span class="mi">42</span><span class="p">,</span> <span class="mi">51</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">headers</span><span class="p">[</span><span class="s1">&#39;Date&#39;</span><span class="p">]</span>
<span class="go">&#39;Fri, 20 Feb 2009 17:42:51 GMT&#39;</span>
</pre></div>
</div>
<p>Because etags can be weak or strong there are methods to set them:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">set_etag</span><span class="p">(</span><span class="s2">&quot;12345-abcd&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">headers</span><span class="p">[</span><span class="s1">&#39;etag&#39;</span><span class="p">]</span>
<span class="go">&#39;&quot;12345-abcd&quot;&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">get_etag</span><span class="p">()</span>
<span class="go">(&#39;12345-abcd&#39;, False)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">set_etag</span><span class="p">(</span><span class="s2">&quot;12345-abcd&quot;</span><span class="p">,</span> <span class="n">weak</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">get_etag</span><span class="p">()</span>
<span class="go">(&#39;12345-abcd&#39;, True)</span>
</pre></div>
</div>
<p>Some headers are available as mutable structures.  For example most
of the <cite>Content-</cite> headers are sets of values:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">content_language</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s1">&#39;en-us&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">content_language</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s1">&#39;en&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">headers</span><span class="p">[</span><span class="s1">&#39;Content-Language&#39;</span><span class="p">]</span>
<span class="go">&#39;en-us, en&#39;</span>
</pre></div>
</div>
<p>Also here this works in both directions:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">headers</span><span class="p">[</span><span class="s1">&#39;Content-Language&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;de-AT, de&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">content_language</span>
<span class="go">HeaderSet([&#39;de-AT&#39;, &#39;de&#39;])</span>
</pre></div>
</div>
<p>Authentication headers can be set that way as well:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">www_authenticate</span><span class="o">.</span><span class="n">set_basic</span><span class="p">(</span><span class="s2">&quot;My protected resource&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">headers</span><span class="p">[</span><span class="s1">&#39;www-authenticate&#39;</span><span class="p">]</span>
<span class="go">&#39;Basic realm=&quot;My protected resource&quot;&#39;</span>
</pre></div>
</div>
<p>Cookies can be set as well:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">set_cookie</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;value&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">headers</span><span class="p">[</span><span class="s1">&#39;Set-Cookie&#39;</span><span class="p">]</span>
<span class="go">&#39;name=value; Path=/&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">set_cookie</span><span class="p">(</span><span class="s1">&#39;name2&#39;</span><span class="p">,</span> <span class="s1">&#39;value2&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>If headers appear multiple times you can use the <code class="xref py py-meth docutils literal notranslate"><span class="pre">getlist()</span></code>
method to get all values for a header:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">headers</span><span class="o">.</span><span class="n">getlist</span><span class="p">(</span><span class="s1">&#39;Set-Cookie&#39;</span><span class="p">)</span>
<span class="go">[&#39;name=value; Path=/&#39;, &#39;name2=value2; Path=/&#39;]</span>
</pre></div>
</div>
<p>Finally if you have set all the conditional values, you can make the
response conditional against a request.  Which means that if the request
can assure that it has the information already, no data besides the headers
is sent over the network which saves traffic.  For that you should set at
least an etag (which is used for comparison) and the date header and then
call <code class="xref py py-class docutils literal notranslate"><span class="pre">make_conditional</span></code> with the request object.</p>
<p>The response is modified accordingly (status code changed, response body
removed, entity headers removed etc.)</p>
</div>
</div>


          </div>
        </div>
      </div>
  <span id="sidebar-top"></span>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  
    
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/werkzeug.png" alt="Logo"/>
            </a></p>
  

  <h3>Contents</h3>
  <ul>
<li><a class="reference internal" href="#">Quickstart</a><ul>
<li><a class="reference internal" href="#wsgi-environment">WSGI Environment</a></li>
<li><a class="reference internal" href="#enter-request">Enter Request</a></li>
<li><a class="reference internal" href="#header-parsing">Header Parsing</a></li>
<li><a class="reference internal" href="#responses">Responses</a></li>
</ul>
</li>
</ul>
<h3>Navigation</h3>
<ul>
  <li><a href="../index.html">Overview</a>
    <ul>
          <li>Previous: <a href="../levels/index.html" title="previous chapter">API Levels</a>
          <li>Next: <a href="../serving/index.html" title="next chapter">Serving WSGI Applications</a>
    </ul>
  </li>
</ul>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://werkzeug.palletsprojects.com/en/1.0.x/search/" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
  
    <div class="footer" role="contentinfo">
        &#169; Copyright 2007 Pallets.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.8.5.
    </div>
    <script>
      if (typeof READTHEDOCS_DATA !== 'undefined') {
        if (!READTHEDOCS_DATA.features) {
          READTHEDOCS_DATA.features = {};
        }
        READTHEDOCS_DATA.features.docsearch_disabled = true;
      }
    </script>

  </body>

<!-- Mirrored from werkzeug.palletsprojects.com/en/1.0.x/quickstart/ by HTTrack Website Copier/3.x [XR&CO'2014], Tue, 15 Sep 2020 06:37:08 GMT -->
</html>